//
//  NetWorkTools.m
//  02-网络
//
//  Created by rimi on 16/5/9.
//  Copyright © 2016年 yyy. All rights reserved.
//

#import "NetWorkTools.h"

//当前单例是封装的三方网络框架,在这个单例类中需要使用三方网络框架的属性和方法.所以需要继承至三方的类

static NetWorkTools * netWorkTools = nil;
@implementation NetWorkTools

//网络 工具单例类方法
+ (instancetype)sharedTools {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        netWorkTools = [[NetWorkTools alloc] initWithBaseURL:nil];
        netWorkTools.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
    });
    return netWorkTools;
}

//封装网络数据请求方法
- (void)requestMethod:(requestMethod)method URL:(NSString *)url parameters:(id)param finish:(callBackBlock)finished{
    if (method == GET) {
        [self GET:url parameters:param success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
            finished(responseObject,nil);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            finished(nil,error);
        }];
        
    } else if (method == POST) {
        [self POST:url parameters:param success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
            finished(responseObject,nil);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            finished(nil,error);
        }];
        
        
    }
}

@end
